﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Core;
using System.Web.UI.HtmlControls;

namespace Yuanlin.Business
{
    public class ContentHtmlList : YuanlinUserControl,
        IReplyConverter<ContentHtmlReply, GetListOperation>
    {
        protected Repeater listLeftMenuName;
        protected Repeater list;
        private EntityList contentHtmls;

        private int categoryId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryId);
        public int entityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ContentHtmlId);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                ContentHtmlListRequest request = new ContentHtmlListRequest();
                request.CategoryId = categoryId;
                request.Title = "";
                request.PageIndex = 0;
                request.PageSize = 100;
                requestBinders.Add(RequestHelper.BuildGetContentHtmlListRequest(this, request));
            }
        }

        private void BindData()
        {
            listLeftMenuName.DataSource = contentHtmls;
            listLeftMenuName.DataBind();
            if (contentHtmls.Count == 0)
            {
                return;
            }

            EntityList entityList = new EntityList()  ;
            ContentHtml contentHtml = null;
            if (entityId == 0)
            {
                contentHtml = contentHtmls[0] as ContentHtml;
            }
            else
            {
                contentHtml = BusinessManager.GetContentHtml(entityId);
                if (contentHtml == null)
                {
                    return;
                }
            }
            entityList.Add(contentHtml);
            list.DataSource = entityList;
            list.DataBind();
        }

        #region IReplyConverter<ContentHtmlReply,GetListOperation> Members

        public void ConvertReply(ContentHtmlReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                contentHtmls = reply.EntityList;

                if (!Page.IsPostBack)
                {
                    BindData();
                }
            }
        }

        #endregion
    }
}